package com.example.Less.service.impl;


import com.example.Less.entity.Language;
import com.example.Less.entity.PageResponse;
import com.example.Less.entity.Photo;
import com.example.Less.entity.PhotoSearchQuery;
import com.example.Less.repository.PhotoRepository;
import com.example.Less.service.PhotoService;
import com.example.Less.util.MinioUtil;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@RequiredArgsConstructor
public class PhotoServiceImpl implements PhotoService {
    
    private final PhotoRepository photoRepository;


    @Resource
    private MinioUtil minioUtil;
    @Override
    public Photo savePhoto(Photo photo) {
        return photoRepository.save(photo);
    }

    @Override
    public Photo getPhotoById(Integer id) {
        return photoRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("Photo not found"));
    }

    @Override
    public List<Photo> getAllPhotos() {
        return photoRepository.findAll();
    }

    @Override
    public Photo updatePhoto(Photo photo) {
        if (!photoRepository.existsById(photo.getId())) {
            throw new RuntimeException("Photo not found");
        }
        return photoRepository.save(photo);
    }

    @Override
    public void deletePhoto(Integer id) {
        photoRepository.deleteById(id);
    }


//    @Override
//    public List<HashMap<String, String>> searchPhotos(String photoName, String photoPath, String type,
//                                                       Integer projectId, Integer isDeleted) {
//        List<Object[]> results =photoRepository.findByConditions(photoName, photoPath, type, projectId, isDeleted);
//        List<HashMap<String, String>> resultMaps = new ArrayList<>();
//
//        for (Object[] result : results) {
//            Photo photo = (Photo) result[0];
//            Language language = (Language) result[1];
//
//            HashMap<String, String> map = new HashMap<>();
//            map.put("photoName", photo.getPhotoName());
//            map.put("photoPath", photo.getPhotoPath());
//            map.put("createTime", photo.getCreateTime()+"");
//            map.put("type", photo.getType());
//            map.put("ch", language.getCh());
//            map.put("en", language.getEn());
//            map.put("ca", language.getCa());
//
//            resultMaps.add(map);
//        }
//        return resultMaps;

 //   }
 @Override
 public PageResponse<Photo> searchPhotos(PhotoSearchQuery query) {
     // 使用PageHelper开始分页
     // 构建分页和排序
     Sort sort = buildSort(query.getOrderBy());
     Pageable pageable = PageRequest.of(query.getPageNum() - 1, query.getPageSize(), sort);

     // 执行分页查询
     Page<Photo> page = photoRepository.searchPhotos(
             query.getPhotoName(),
             query.getPhotoPath(),
             query.getType(),
             query.getProjectId(),
             query.getIsDeleted(),
             pageable
     );

     return new PageResponse<>(
             page.getContent(),
             page.getTotalElements(),
             query.getPageNum(),
             query.getPageSize()
     );
 }
    /**
     * 构建排序对象
     */
    private Sort buildSort(String orderBy) {
        if (orderBy == null || orderBy.trim().isEmpty()) {
            return Sort.by(Sort.Direction.DESC, "createTime");
        }

        String[] parts = orderBy.split(" ");
        if (parts.length == 2) {
            Sort.Direction direction = parts[1].equalsIgnoreCase("asc") ?
                    Sort.Direction.ASC : Sort.Direction.DESC;
            return Sort.by(direction, parts[0]);
        }

        return Sort.by(Sort.Direction.DESC, "createTime");
    }
}